Preliminaries

Assuming either

  • Linux (Ubuntu)
  • macOS

though Windows is still doable; the parts about the PATH variables may or may not be relevant.

Part 1: Installing Anaconda

Anaconda is an incredibly popular Python distribution platform. It's the product of Continuum Analytics, and is effectively the de facto way to interact with the Python ecosystem at this point.

(Translation: Use Anaconda for installing Python. DO NOT USE python.org unless you are super comfortable with micromanaging your Python install and the packages you download)

Installing Anaconda is easy enough: navigate to this link https://www.continuum.io/downloads

The website will auto-detect your operating system and present you with the relevant downloads (but of course you can pick another if you want).

Since I do pretty much everything by command line these days (and Jupyter notebooks provide a direct interface to the command line!), I'll go with the Linux version of the installer (but again, the Windows exe and macOS pkg will provide a GUI installer if you really want it).


In [1]:
# Step 1: right-click the "download" link on the left

# Step 2: select "copy link address"

# Step 3: paste the link into the following bash command, after "wget"

!wget https://repo.continuum.io/archive/Anaconda3-4.4.0-Linux-x86_64.sh


--2017-08-11 19:50:27--  https://repo.continuum.io/archive/Anaconda3-4.4.0-Linux-x86_64.sh
Resolving repo.continuum.io (repo.continuum.io)... 104.16.18.10, 104.16.19.10, 2400:cb00:2048:1::6810:130a, ...
Connecting to repo.continuum.io (repo.continuum.io)|104.16.18.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 523283080 (499M) [application/x-sh]
Saving to: ‘Anaconda3-4.4.0-Linux-x86_64.sh’

Anaconda3-4.4.0-Lin 100%[===================>] 499.04M   105MB/s    in 5.7s    

2017-08-11 19:50:32 (88.0 MB/s) - ‘Anaconda3-4.4.0-Linux-x86_64.sh’ saved [523283080/523283080]

The download takes awhile; it's a big distribution!

Once it's downloaded, all you have to provide is an install prefix, i.e. where you want this Python install to live on your computer.

  • I like to install mine at /opt/conda, but that's a global configuration.
  • You can just as easily do a local one at /home/<your_username>/conda or /home/<your_username>/python or whatever you'd like!

In [2]:
!ls # This will show us the files in our current directory


Anaconda3-4.4.0-Linux-x86_64.sh
images
W0--Getting Started with Anaconda for Data Science in Python.ipynb

In [3]:
# This is an easy one-liner, but it's absolutely necessary: it makes the file *executable*

!chmod +x Anaconda3-4.4.0-Linux-x86_64.sh

In [4]:
# The "-b" flag means "batch", which means the install won't stop to ask us pesky questions
# The "-p" flag expects a path where Python will be installed. I've provided a local one

!bash Anaconda3-4.4.0-Linux-x86_64.sh -b -p ./conda-install


PREFIX=/home/squinn/Programming/Teaching/4360/dev/workshops/w0/conda-install
installing: python-3.6.1-2 ...
installing: _license-1.1-py36_1 ...
installing: alabaster-0.7.10-py36_0 ...
installing: anaconda-client-1.6.3-py36_0 ...
installing: anaconda-navigator-1.6.2-py36_0 ...
installing: anaconda-project-0.6.0-py36_0 ...
installing: asn1crypto-0.22.0-py36_0 ...
installing: astroid-1.4.9-py36_0 ...
installing: astropy-1.3.2-np112py36_0 ...
installing: babel-2.4.0-py36_0 ...
installing: backports-1.0-py36_0 ...
installing: beautifulsoup4-4.6.0-py36_0 ...
installing: bitarray-0.8.1-py36_0 ...
installing: blaze-0.10.1-py36_0 ...
installing: bleach-1.5.0-py36_0 ...
installing: bokeh-0.12.5-py36_1 ...
installing: boto-2.46.1-py36_0 ...
installing: bottleneck-1.2.1-np112py36_0 ...
installing: cairo-1.14.8-0 ...
installing: cffi-1.10.0-py36_0 ...
installing: chardet-3.0.3-py36_0 ...
installing: click-6.7-py36_0 ...
installing: cloudpickle-0.2.2-py36_0 ...
installing: clyent-1.2.2-py36_0 ...
installing: colorama-0.3.9-py36_0 ...
installing: contextlib2-0.5.5-py36_0 ...
installing: cryptography-1.8.1-py36_0 ...
installing: curl-7.52.1-0 ...
installing: cycler-0.10.0-py36_0 ...
installing: cython-0.25.2-py36_0 ...
installing: cytoolz-0.8.2-py36_0 ...
installing: dask-0.14.3-py36_1 ...
installing: datashape-0.5.4-py36_0 ...
installing: dbus-1.10.10-0 ...
installing: decorator-4.0.11-py36_0 ...
installing: distributed-1.16.3-py36_0 ...
installing: docutils-0.13.1-py36_0 ...
installing: entrypoints-0.2.2-py36_1 ...
installing: et_xmlfile-1.0.1-py36_0 ...
installing: expat-2.1.0-0 ...
installing: fastcache-1.0.2-py36_1 ...
installing: flask-0.12.2-py36_0 ...
installing: flask-cors-3.0.2-py36_0 ...
installing: fontconfig-2.12.1-3 ...
installing: freetype-2.5.5-2 ...
installing: get_terminal_size-1.0.0-py36_0 ...
installing: gevent-1.2.1-py36_0 ...
installing: glib-2.50.2-1 ...
installing: greenlet-0.4.12-py36_0 ...
installing: gst-plugins-base-1.8.0-0 ...
installing: gstreamer-1.8.0-0 ...
installing: h5py-2.7.0-np112py36_0 ...
installing: harfbuzz-0.9.39-2 ...
installing: hdf5-1.8.17-1 ...
installing: heapdict-1.0.0-py36_1 ...
installing: html5lib-0.999-py36_0 ...
installing: icu-54.1-0 ...
installing: idna-2.5-py36_0 ...
installing: imagesize-0.7.1-py36_0 ...
installing: ipykernel-4.6.1-py36_0 ...
installing: ipython-5.3.0-py36_0 ...
installing: ipython_genutils-0.2.0-py36_0 ...
installing: ipywidgets-6.0.0-py36_0 ...
installing: isort-4.2.5-py36_0 ...
installing: itsdangerous-0.24-py36_0 ...
installing: jbig-2.1-0 ...
installing: jdcal-1.3-py36_0 ...
installing: jedi-0.10.2-py36_2 ...
installing: jinja2-2.9.6-py36_0 ...
installing: jpeg-9b-0 ...
installing: jsonschema-2.6.0-py36_0 ...
installing: jupyter-1.0.0-py36_3 ...
installing: jupyter_client-5.0.1-py36_0 ...
installing: jupyter_console-5.1.0-py36_0 ...
installing: jupyter_core-4.3.0-py36_0 ...
installing: lazy-object-proxy-1.2.2-py36_0 ...
installing: libffi-3.2.1-1 ...
installing: libgcc-4.8.5-2 ...
installing: libgfortran-3.0.0-1 ...
installing: libiconv-1.14-0 ...
installing: libpng-1.6.27-0 ...
installing: libsodium-1.0.10-0 ...
installing: libtiff-4.0.6-3 ...
installing: libtool-2.4.2-0 ...
installing: libxcb-1.12-1 ...
installing: libxml2-2.9.4-0 ...
installing: libxslt-1.1.29-0 ...
installing: llvmlite-0.18.0-py36_0 ...
installing: locket-0.2.0-py36_1 ...
installing: lxml-3.7.3-py36_0 ...
installing: markupsafe-0.23-py36_2 ...
installing: matplotlib-2.0.2-np112py36_0 ...
installing: mistune-0.7.4-py36_0 ...
installing: mkl-2017.0.1-0 ...
installing: mkl-service-1.1.2-py36_3 ...
installing: mpmath-0.19-py36_1 ...
installing: msgpack-python-0.4.8-py36_0 ...
installing: multipledispatch-0.4.9-py36_0 ...
installing: navigator-updater-0.1.0-py36_0 ...
installing: nbconvert-5.1.1-py36_0 ...
installing: nbformat-4.3.0-py36_0 ...
installing: networkx-1.11-py36_0 ...
installing: nltk-3.2.3-py36_0 ...
installing: nose-1.3.7-py36_1 ...
installing: notebook-5.0.0-py36_0 ...
installing: numba-0.33.0-np112py36_0 ...
installing: numexpr-2.6.2-np112py36_0 ...
installing: numpy-1.12.1-py36_0 ...
installing: numpydoc-0.6.0-py36_0 ...
installing: odo-0.5.0-py36_1 ...
installing: olefile-0.44-py36_0 ...
installing: openpyxl-2.4.7-py36_0 ...
installing: openssl-1.0.2l-0 ...
installing: packaging-16.8-py36_0 ...
installing: pandas-0.20.1-np112py36_0 ...
installing: pandocfilters-1.4.1-py36_0 ...
installing: pango-1.40.3-1 ...
installing: partd-0.3.8-py36_0 ...
installing: path.py-10.3.1-py36_0 ...
installing: pathlib2-2.2.1-py36_0 ...
installing: patsy-0.4.1-py36_0 ...
installing: pcre-8.39-1 ...
installing: pep8-1.7.0-py36_0 ...
installing: pexpect-4.2.1-py36_0 ...
installing: pickleshare-0.7.4-py36_0 ...
installing: pillow-4.1.1-py36_0 ...
installing: pip-9.0.1-py36_1 ...
installing: pixman-0.34.0-0 ...
installing: ply-3.10-py36_0 ...
installing: prompt_toolkit-1.0.14-py36_0 ...
installing: psutil-5.2.2-py36_0 ...
installing: ptyprocess-0.5.1-py36_0 ...
installing: py-1.4.33-py36_0 ...
installing: pycosat-0.6.2-py36_0 ...
installing: pycparser-2.17-py36_0 ...
installing: pycrypto-2.6.1-py36_6 ...
installing: pycurl-7.43.0-py36_2 ...
installing: pyflakes-1.5.0-py36_0 ...
installing: pygments-2.2.0-py36_0 ...
installing: pylint-1.6.4-py36_1 ...
installing: pyodbc-4.0.16-py36_0 ...
installing: pyopenssl-17.0.0-py36_0 ...
installing: pyparsing-2.1.4-py36_0 ...
installing: pyqt-5.6.0-py36_2 ...
installing: pytables-3.3.0-np112py36_0 ...
installing: pytest-3.0.7-py36_0 ...
installing: python-dateutil-2.6.0-py36_0 ...
installing: pytz-2017.2-py36_0 ...
installing: pywavelets-0.5.2-np112py36_0 ...
installing: pyyaml-3.12-py36_0 ...
installing: pyzmq-16.0.2-py36_0 ...
installing: qt-5.6.2-4 ...
installing: qtawesome-0.4.4-py36_0 ...
installing: qtconsole-4.3.0-py36_0 ...
installing: qtpy-1.2.1-py36_0 ...
installing: readline-6.2-2 ...
installing: requests-2.14.2-py36_0 ...
installing: rope-0.9.4-py36_1 ...
installing: ruamel_yaml-0.11.14-py36_1 ...
installing: scikit-image-0.13.0-np112py36_0 ...
installing: scikit-learn-0.18.1-np112py36_1 ...
installing: scipy-0.19.0-np112py36_0 ...
installing: seaborn-0.7.1-py36_0 ...
installing: setuptools-27.2.0-py36_0 ...
installing: simplegeneric-0.8.1-py36_1 ...
installing: singledispatch-3.4.0.3-py36_0 ...
installing: sip-4.18-py36_0 ...
installing: six-1.10.0-py36_0 ...
installing: snowballstemmer-1.2.1-py36_0 ...
installing: sortedcollections-0.5.3-py36_0 ...
installing: sortedcontainers-1.5.7-py36_0 ...
installing: sphinx-1.5.6-py36_0 ...
installing: spyder-3.1.4-py36_0 ...
installing: sqlalchemy-1.1.9-py36_0 ...
installing: sqlite-3.13.0-0 ...
installing: statsmodels-0.8.0-np112py36_0 ...
installing: sympy-1.0-py36_0 ...
installing: tblib-1.3.2-py36_0 ...
installing: terminado-0.6-py36_0 ...
installing: testpath-0.3-py36_0 ...
installing: tk-8.5.18-0 ...
installing: toolz-0.8.2-py36_0 ...
installing: tornado-4.5.1-py36_0 ...
installing: traitlets-4.3.2-py36_0 ...
installing: unicodecsv-0.14.1-py36_0 ...
installing: unixodbc-2.3.4-0 ...
installing: wcwidth-0.1.7-py36_0 ...
installing: werkzeug-0.12.2-py36_0 ...
installing: wheel-0.29.0-py36_0 ...
installing: widgetsnbextension-2.0.0-py36_0 ...
installing: wrapt-1.10.10-py36_0 ...
installing: xlrd-1.0.0-py36_0 ...
installing: xlsxwriter-0.9.6-py36_0 ...
installing: xlwt-1.2.0-py36_0 ...
installing: xz-5.2.2-1 ...
installing: yaml-0.1.6-0 ...
installing: zeromq-4.1.5-0 ...
installing: zict-0.1.2-py36_0 ...
installing: zlib-1.2.8-3 ...
installing: anaconda-4.4.0-np112py36_0 ...
installing: conda-4.3.21-py36_0 ...
installing: conda-env-2.6.0-0 ...
Python 3.6.1 :: Continuum Analytics, Inc.
creating default environment...
installation finished.

This part will take awhile, depending largely on your internet connection. Go grab some coffee!

There's one more small step to be done, and here's where things might get confusing if you've little or no experience installing things by command line.

You've installed Python locally, and that's super-cool--uninstall is quite literally as easy as trashing the directory you put after the -p flag--but the problem is, your computer doesn't know where it is.


In [5]:
!which python


/opt/python/bin/python

This is a great command to test exactly what version of python is being executed. In this case, it's a version I'd installed previous, at /opt/python. That is definitely not the version I just installed. How do I alert my operating system that, when I type a command starting with python, I want it to refer to the local one I just installed?

You have two options: both involve editing the PATH environment variable that is more or less singularly responsible for telling your computer where everything is (if you've ever accidentally nuked this variable, you know how important it is). The difference is how your edits persist.

Option 1: Edit your .bashrc (*nix) or .profile (macOS)

This makes the changes stick, even after you close down your command prompt. Even after you reboot!

  1. Open up the correct file (they're both found in your home directory).
  2. At the end of the file, put the following line:
export PATH=/home/<your_username>/conda-install/bin:$PATH

Basically, the FULL path of whatever directory you provided to the Anaconda installer, with /bin tacked on the end.

Also, don't forget the colon and the $PATH suffix! Doing so will achieve said nuking of the variable!

Option 2: DO IT LIVE

This is essentially: changing the variable for the duration of time you have your command prompt window open. Once you close it down, your computer reverts to whatever configuration it was before. That can be achieved simply by running the above command right in your command prompt window:

export PATH=/home/<your_username>/conda-install/bin:$PATH

I'll do that one here, using the Terminal interface provided by Jupyter notebooks:

Once you type exit on that command prompt, those changes will go away and the PATH variable will revert to whatever it was before, obviously devoid of the changes you made with respect to the local Python install. So if you want the changes to stick, go with Option 1.

Option 3: It's done for you

If you use the exe or pkg installers provided by the Windows or macOS versions respectively, they do this exact thing under the hood!

Part 2: Conda Environments

One of the most powerful aspects of Anaconda is the concept of the environment.

Have you heard of virtualenv? It's a Python package that allows to create "virtual environments" that are completely disconnected from each other. Reasons you might want to have parallel Python environments existing completely separately from one another:

  • Testing version compatibility of a software package you're developing
  • Using a third-party package that is only compatible with certain versions of other packages
  • Slowly upgrading aspects of an environment while wanting to maintain a working, original "production" environment
  • ... etc

The conda tool, first and foremost, is an environment manager. In fact, when you installed Anaconda, it created an initial "default" environment: using this blueprint as a jumping-off point, you could construct lots of parallel environments using different versions of Python, or the packages, or just different package combinations.

Biggest example: Python 2 versus Python 3.


In [4]:
!conda env list


# conda environments:
#
py27                     /opt/python/envs/py27
py35-opencv3             /opt/python/envs/py35-opencv3
py36                     /opt/python/envs/py36
root                  *  /opt/python

As you can see, I already have four Python environments up and running for several different versions of Python.

Creating a new "ad-hoc" environment

This is certainly the easiest way of creating a new environment: just give conda a name and a list of core packages, and you're good to go!


In [6]:
!conda create -n myenv -y python=2.7 scikit-learn numpy scipy matplotlib


Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment /opt/python/envs/myenv:

The following NEW packages will be INSTALLED:

    backports_abc:      0.5-py27_0                    conda-forge
    blas:               1.1-openblas                  conda-forge
    ca-certificates:    2017.7.27.1-0                 conda-forge
    certifi:            2017.7.27.1-py27_0            conda-forge
    cycler:             0.10.0-py27_0                 conda-forge
    dbus:               1.10.10-3                     conda-forge
    expat:              2.2.1-0                       conda-forge
    fontconfig:         2.12.1-4                      conda-forge
    freetype:           2.7-1                         conda-forge
    functools32:        3.2.3.2-py27_1                conda-forge
    gettext:            0.19.7-1                      conda-forge
    glib:               2.51.4-0                      conda-forge
    gst-plugins-base:   1.8.0-0                       conda-forge
    gstreamer:          1.8.0-2                       conda-forge
    icu:                58.1-1                        conda-forge
    jpeg:               9b-0                          conda-forge
    libffi:             3.2.1-3                       conda-forge
    libgfortran:        3.0.0-1                                  
    libiconv:           1.14-4                        conda-forge
    libpng:             1.6.28-0                      conda-forge
    libxcb:             1.12-1                        conda-forge
    libxml2:            2.9.4-4                       conda-forge
    matplotlib:         2.0.2-py27_2                  conda-forge
    ncurses:            5.9-10                        conda-forge
    numpy:              1.13.1-py27_blas_openblas_200 conda-forge [blas_openblas]
    openblas:           0.2.19-2                      conda-forge
    openssl:            1.0.2l-0                      conda-forge
    pcre:               8.39-0                        conda-forge
    pip:                9.0.1-py27_0                  conda-forge
    pyparsing:          2.2.0-py27_0                  conda-forge
    pyqt:               5.6.0-py27_4                  conda-forge
    python:             2.7.13-1                      conda-forge
    python-dateutil:    2.6.1-py27_0                  conda-forge
    pytz:               2017.2-py27_0                 conda-forge
    qt:                 5.6.2-3                       conda-forge
    readline:           6.2-0                         conda-forge
    scikit-learn:       0.19.0-py27_blas_openblas_201 conda-forge [blas_openblas]
    scipy:              0.19.1-py27_blas_openblas_202 conda-forge [blas_openblas]
    setuptools:         36.2.2-py27_0                 conda-forge
    singledispatch:     3.4.0.3-py27_0                conda-forge
    sip:                4.18-py27_1                   conda-forge
    six:                1.10.0-py27_1                 conda-forge
    sqlite:             3.13.0-1                      conda-forge
    ssl_match_hostname: 3.5.0.1-py27_1                conda-forge
    subprocess32:       3.2.7-py27_0                  conda-forge
    tk:                 8.5.19-2                      conda-forge
    tornado:            4.5.1-py27_0                  conda-forge
    wheel:              0.29.0-py27_0                 conda-forge
    xorg-libxau:        1.0.8-3                       conda-forge
    xorg-libxdmcp:      1.1.2-3                       conda-forge
    xz:                 5.2.2-0                       conda-forge
    zlib:               1.2.11-0                      conda-forge

ca-certificate 100% |################################| Time: 0:00:00   1.16 MB/s
tk-8.5.19-2.ta 100% |################################| Time: 0:00:00   7.57 MB/s
python-2.7.13- 100% |################################| Time: 0:00:00  38.14 MB/s
backports_abc- 100% |################################| Time: 0:00:00  12.50 MB/s
certifi-2017.7 100% |################################| Time: 0:00:00  75.22 MB/s
functools32-3. 100% |################################| Time: 0:00:00  31.36 MB/s
numpy-1.13.1-p 100% |################################| Time: 0:00:00  44.09 MB/s
pyparsing-2.2. 100% |################################| Time: 0:00:00  64.14 MB/s
pytz-2017.2-py 100% |################################| Time: 0:00:00  72.18 MB/s
sip-4.18-py27_ 100% |################################| Time: 0:00:00  74.45 MB/s
six-1.10.0-py2 100% |################################| Time: 0:00:00   1.53 MB/s
ssl_match_host 100% |################################| Time: 0:00:00  17.21 MB/s
subprocess32-3 100% |################################| Time: 0:00:00  50.41 MB/s
cycler-0.10.0- 100% |################################| Time: 0:00:00  25.00 MB/s
python-dateuti 100% |################################| Time: 0:00:00  11.84 MB/s
scipy-0.19.1-p 100% |################################| Time: 0:00:00  69.25 MB/s
setuptools-36. 100% |################################| Time: 0:00:00  90.08 MB/s
singledispatch 100% |################################| Time: 0:00:00  16.12 MB/s
pyqt-5.6.0-py2 100% |################################| Time: 0:00:00  25.71 MB/s
scikit-learn-0 100% |################################| Time: 0:00:00  64.01 MB/s
tornado-4.5.1- 100% |################################| Time: 0:00:00  28.77 MB/s
matplotlib-2.0 100% |################################| Time: 0:00:00  40.42 MB/s
#
# To activate this environment, use:
# > source activate myenv
#
# To deactivate an active environment, use:
# > source deactivate
#

Ta-daa!

Let's break it down:

conda create

This is the part of the command that specifies that we want a new environment, separate from the default (or "root").

-n myenv

This part provides the name of the environment. It can be pretty much anything other than "root", aside from any other environments you've named. Try to make the name descriptive of its purpose! (HINT: perhaps name your environments by the project you're working on?...)

python=2.7 scikit-learn numpy scipy matplotlib

This is the list of packages I want my new environment to come with by default: Python (version 2.7), scikit-learn, NumPy, SciPy, and Matplotlib.

After that, conda goes to work! It even provides the instructions at the end for switching into and out of the environment:

source activate myenv

drops you into the environment, and once inside it,

source deactivate

drops you out and back into the default, or root, environment.


In [7]:
!conda env list


# conda environments:
#
myenv                    /opt/python/envs/myenv
py27                     /opt/python/envs/py27
py35-opencv3             /opt/python/envs/py35-opencv3
py36                     /opt/python/envs/py36
root                  *  /opt/python

Addendum: Cloning an existing environment

If you love an environment and have it set up perfectly and want to introduce an unstable element, you can also clone that environment into a new one.

It works the same as before, but in lieu of a package list (which you can still provide!), the main argument is the --clone option:


In [8]:
!conda create -n myenv2 --clone myenv


Source:      /opt/python/envs/myenv
Destination: /opt/python/envs/myenv2
Packages: 52
Files: 0
#
# To activate this environment, use:
# > source activate myenv2
#
# To deactivate an active environment, use:
# > source deactivate
#


In [9]:
!conda env list


# conda environments:
#
myenv                    /opt/python/envs/myenv
myenv2                   /opt/python/envs/myenv2
py27                     /opt/python/envs/py27
py35-opencv3             /opt/python/envs/py35-opencv3
py36                     /opt/python/envs/py36
root                  *  /opt/python

There you go! Two identical but completely distinct environments.

Exporting environments

You've gone through all this trouble to set up and configure your environment. Over the days/weeks/months, you've installed and removed certain packages. You have the perfect environment.

Now, your friend wants the same environment. We've seen how easy it is to clone existing environments on the same machine, but what about sending the environment schematics elsewhere?

As an example, I maintain a list of Python packages that I use to rebuild my default environment from scratch in the case of catastrophic failure.

In this case, you can export your environments into the super-friendly YAML format:


In [1]:
!conda env export -n myenv2 -f myenv2.yaml

This creates a file myenv2.yaml (which you can name whatever you want) from the "myenv2" environment. Here are the contents of the file:


In [ ]:
# %load myenv2.yaml
name: myenv2
channels:
- menpo
- conda-forge
- defaults
dependencies:
- backports_abc=0.5=py27_0
- blas=1.1=openblas
- ca-certificates=2017.7.27.1=0
- certifi=2017.7.27.1=py27_0
- cycler=0.10.0=py27_0
- dbus=1.10.10=3
- expat=2.2.1=0
- fontconfig=2.12.1=4
- freetype=2.7=1
- functools32=3.2.3.2=py27_1
- gettext=0.19.7=1
- glib=2.51.4=0
- gst-plugins-base=1.8.0=0
- gstreamer=1.8.0=2
- icu=58.1=1
- jpeg=9b=0
- libffi=3.2.1=3
- libiconv=1.14=4
- libpng=1.6.28=0
- libxcb=1.12=1
- libxml2=2.9.4=4
- matplotlib=2.0.2=py27_2
- ncurses=5.9=10
- numpy=1.13.1=py27_blas_openblas_200
- openblas=0.2.19=2
- openssl=1.0.2l=0
- pcre=8.39=0
- pip=9.0.1=py27_0
- pyparsing=2.2.0=py27_0
- pyqt=5.6.0=py27_4
- python=2.7.13=1
- python-dateutil=2.6.1=py27_0
- pytz=2017.2=py27_0
- qt=5.6.2=3
- readline=6.2=0
- scikit-learn=0.19.0=py27_blas_openblas_201
- scipy=0.19.1=py27_blas_openblas_202
- setuptools=36.2.2=py27_0
- singledispatch=3.4.0.3=py27_0
- sip=4.18=py27_1
- six=1.10.0=py27_1
- sqlite=3.13.0=1
- ssl_match_hostname=3.5.0.1=py27_1
- subprocess32=3.2.7=py27_0
- tk=8.5.19=2
- tornado=4.5.1=py27_0
- wheel=0.29.0=py27_0
- xorg-libxau=1.0.8=3
- xorg-libxdmcp=1.1.2=3
- xz=5.2.2=0
- zlib=1.2.11=0
- libgfortran=3.0.0=1
- pip:
  - backports-abc==0.5
  - backports.ssl-match-hostname==3.5.0.1
prefix: /opt/python/envs/myenv2

It's incredibly detailed, down to the exact versions of each package used in the environment. Short of the operating system itself, you can use this to duplicate environments exactly on different computers.

Creating manually-built environments

Then, of course, there's the opposite operation: creating an environment from a YAML file.

NOTE: You'll notice there's a name field in the YAML file. Since we're performing both the export and import on the same machine, there could be a conflict in the name of the environment. Normally, this wouldn't be the case, but here we'll have to fix that one of two ways.

1: Just change the name of the environment in the YAML file to something else. 2: Use the -n flag to specify a name when you create the environment from the file.


In [3]:
!conda env create -n myenv3 -f myenv2.yaml


Fetching package metadata .............
Solving package specifications: .
Requirement already satisfied: backports-abc==0.5 in /opt/python/envs/myenv3/lib/python2.7/site-packages
Requirement already satisfied: backports.ssl-match-hostname==3.5.0.1 in /opt/python/envs/myenv3/lib/python2.7/site-packages
#
# To activate this environment, use:
# > source activate myenv3
#
# To deactivate an active environment, use:
# > source deactivate
#


In [4]:
!conda env list


# conda environments:
#
myenv                    /opt/python/envs/myenv
myenv2                   /opt/python/envs/myenv2
myenv3                   /opt/python/envs/myenv3
py27                     /opt/python/envs/py27
py35-opencv3             /opt/python/envs/py35-opencv3
py36                     /opt/python/envs/py36
root                  *  /opt/python

Just look at how they multiply!

Part 3: The package manager

Ok, you've got Anaconda installed! You either cloned an existing environment, ripped an environment configuration off someone else, or built a new environment from scratch.

Let's customize it!

conda install

This is the workhorse: you run this command to install a package.

Note that conda will also determine if any other packages (dependencies) need to be installed in order to install the package you requested. Once you've agreed, just hit "y" and the install will proceed.

conda remove

The opposite of the previous command, this will remove the specified packages from your system (note: it won't outright delete them).

Also note: it won't delete any prerequisites it had to install.

Channels

Ok, the last real wrinkle in a conda environment, and where (in my view) conda really gets it power: channels.

The philosophy behind conda--an open source package manager not just for Python, but a language-agnostic package manager that worked irrespective of platform, providing fully-built packages for any operating system--was a good one.

Problem is, there are too many platforms out there to fully satisfy them all. Furthermore, with Continuum being the ones to have to rebuild the packages and dependency lists any time an updated package under the Anaconda purview was released, it got to be too much work.

So they developed the idea of channels: distinct build avenues that even the common user can hook into. You can build a package from scratch with highly customized build options, then put that package into a custom channel.

This strategy has reached fever pitch with the conda-forge project.

The goal of this project is to essentially outsource the building of all Python packages to the community, and make them available through the "conda-forge" channel.

You may have noticed several instances throughout this workshop where the output of certain conda commands contained "conda-forge". I am indeed subscribed to this channel, and I highly recommend you do as well. This will give you access to the most recent build recipes, which are often out very shortly after the new version of any given Python package is released; much quicker turnaround than Anaconda's official default channel.

To subscribe to a channel, you can use the "conda config" command:

Or, in this case:

conda config --add channels conda-forge

Alternatively, you can install packages directly from channels without needing to be subscribed. You can do this through the "-c" flag in the "conda install" command:

conda install -c conda-forge scipy

This command will install the latest version of SciPy, but specifically from the conda-forge channel.

If you don't specify a channel, conda will automatically select the best matching package from a channel you're subscribed to. This is not necessarily the default Anaconda channel! You can actually specify a hierarchy in the channels you're subscribed to--for instance, conda-forge is my highest-level channel, so whenever I do not specify a channel during install, it defaults to conda-forge first before any others.

You can find out this hierarchy from the command:

conda config --show-sources

Keeping your environment up to date

The last command to know--and to use frequently--is conda update. This will update the specified packages to their latest versions available through the corresponding channel.

You can specify a package, e.g.

conda update scikit-learn

Or, you can do the "catch-all" to update absolutely everything:

conda update --all

Just be careful with that last one--I've had strange things happen on occasion...

Part 4: Where to learn more

There are lots of online resources to learn about the ins and outs of conda. I would specifically point you to the following:

Happy coding!